package org.krtrain.sports.dashboard;

import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.krtrain.sports.tracker.Game;
import org.kuali.rice.krad.service.BusinessObjectService;
import org.kuali.rice.krad.service.KRADServiceLocator;
import org.kuali.rice.krad.web.controller.UifControllerBase;
import org.kuali.rice.krad.web.form.UifFormBase;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Kuali Rice Team (rice.collab@kuali.org)
 */
@Controller
@RequestMapping(value = "/gamedashboard")
public class GameDashboardController extends UifControllerBase {

    @Override
    protected GameDashboardForm createInitialForm(HttpServletRequest request) {
        return new GameDashboardForm();
    }

    /**
     * Initial method called when requesting a new view instance which forwards
     * the view for rendering
     */
    @Override
    @RequestMapping(params = "methodToCall=start")
    public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
            HttpServletRequest request, HttpServletResponse response) {
        BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
        Collection<Game> games = businessObjectService.findAll(Game.class);
        ((GameDashboardForm)form).setTodaysGames(new ArrayList<Game>(games));
        return getUIFModelAndView(form);
    }

}
